﻿package kr.co.nextree.android.sample.activity;

import java.util.List;

import kr.co.nextree.android.sample.R;
import kr.co.nextree.android.sample.domain.BusinessCard;
import kr.co.nextree.android.sample.provider.BusinessCardProvider;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class BusinessCardActivity extends Activity {
    private static final int MENU_ADD = 1;
    private static final int MENU_MODIFY = 2;
    private static final int MENU_REMOVE = 3;

    private ListView lvCards;

    /** 어댑터뷰에 데이터를 제공하는 어댑터 객체 */
    private BusinessCardAdapter adapter;

    /** 데이터 관리를 담당하는 객체 */
    private BusinessCardProvider provider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.businesscard_list_layout);

        lvCards = (ListView) findViewById(R.id.list);

        provider = BusinessCardProvider.getInstance();

        // 데이터 조회
        List<BusinessCard> cards = provider.findAllBusinessCard();

        // 어댑터를 생성하여 listView에 주입
        adapter = new BusinessCardAdapter(this, R.layout.businesscard_list_layout_row, cards);
        lvCards.setAdapter(adapter);
        lvCards.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(view.getContext(), "항목선택", Toast.LENGTH_SHORT).show();
			}
		});

        // 컨텍스트메뉴 등록
        registerForContextMenu(lvCards);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_ADD, 0, "추가").setIcon(android.R.drawable.ic_menu_add);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch (item.getItemId()) {
        case MENU_ADD: {
            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            final View view = inflater.inflate(R.layout.businesscard_dialog, null);

            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("명함추가");
            dialog.setView(view);
            dialog.setPositiveButton("확인", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO 1.dialog의 뷰에 입력된 값을 가져온다.
                    EditText etName = (EditText) view.findViewById(R.id.etName);
                    EditText etCompany = (EditText) view.findViewById(R.id.etCompany);
                    EditText etPhoneNo = (EditText) view.findViewById(R.id.etPhoneNo);
                    EditText etEmail = (EditText) view.findViewById(R.id.etEmail);

                    String name = etName.getText().toString();
                    String company = etCompany.getText().toString();
                    String phoneNo = etPhoneNo.getText().toString();
                    String email = etEmail.getText().toString();

                    // TODO 2.BusinessCard 객체를 만든다.
                    BusinessCard card = new BusinessCard();
                    card.setName(name);
                    card.setCompany(company);
                    card.setPhoneNo(phoneNo);
                    card.setEmail(email);

                    // TODO 3.db에 추가
                    provider.registerBusinessCard(card);
                    adapter.setItems(provider.findAllBusinessCard());
                    adapter.notifyDataSetChanged();

                    Toast.makeText(getBaseContext(), "추가합니다.", Toast.LENGTH_SHORT).show();
                }
            });
            dialog.setNegativeButton("취소", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO nothing 다이얼로그 버튼 이벤트에 아무런 작업도 처리하지 않으면 기본동작인 다이얼로그를
                    // 닫는다.
                }
            });

            dialog.show();
            break;
        }
        }

        return false;
    }

    // 컨텍스트 메뉴 생성
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        menu.add(0, MENU_MODIFY, Menu.NONE, "수정");
        menu.add(0, MENU_REMOVE, Menu.NONE, "삭제");
    }

    // 컨텍스트 메뉴 이벤트 처리
    @Override
    public boolean onContextItemSelected(MenuItem item) {

        super.onContextItemSelected(item);
        final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
        case MENU_MODIFY: {
            LayoutInflater inflater = getLayoutInflater();
            final View view = inflater.inflate(R.layout.businesscard_dialog, null);
            final EditText etName = (EditText) view.findViewById(R.id.etName);
            final EditText etCompany = (EditText) view.findViewById(R.id.etCompany);
            final EditText etPhoneNo = (EditText) view.findViewById(R.id.etPhoneNo);
            final EditText etEmail = (EditText) view.findViewById(R.id.etEmail);

            BusinessCard card = provider.getBusinessCard(info.id);
            etName.setText(card.getName());
            etCompany.setText(card.getCompany());
            etPhoneNo.setText(card.getPhoneNo());
            etEmail.setText(card.getEmail());

            new AlertDialog.Builder(this).setTitle("명함수정").setView(view).setPositiveButton("확인", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    BusinessCard card = new BusinessCard();
                    card.setId(info.id);
                    card.setName(etName.getText().toString());
                    card.setCompany(etCompany.getText().toString());
                    card.setPhoneNo(etPhoneNo.getText().toString());
                    card.setEmail(etEmail.getText().toString());

                    provider.modifyBusinessCard(card);
                    adapter.setItems(provider.findAllBusinessCard());
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getBaseContext(), "수정합니다.", Toast.LENGTH_SHORT).show();

                }
            }).setNegativeButton("취소", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }

            }).show();
            return true;
        }
        case MENU_REMOVE: {
            provider.removeBusinessCard(info.id);
            adapter.setItems(provider.findAllBusinessCard());
            adapter.notifyDataSetChanged();
            Toast.makeText(getBaseContext(), "삭제합니다.", Toast.LENGTH_SHORT).show();
            return true;
        }
        }

        return false; // 어떤 context메뉴도 처리하지 않으면 false리턴하여 작업 중단.
    }

    @SuppressWarnings("unused")
    private void hideKeyboard(EditText editText) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
    
    
}
